Skip to content

Fixed compiler output capture in JSON mode #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 10, 2023

Conversation

cmaglie
Copy link
Member

@cmaglie cmaglie commented Feb 17, 2023

Please check if the PR fulfills these requirements

See how to contribute

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • UPGRADING.md has been updated with a migration guide (for breaking changes)

What kind of change does this PR introduce?

In some cases, while running a compile with JSON output format, the stderr of the compiler is lost.
See #1698 for details

What is the current behavior?

$ cat Blink.ino 

void setup() {
}

void loop() {
}
$ cat test.cpp 

void aaa() {

$ arduino-cli compile -b arduino:avr:uno --format json | jq .compiler_err
"\n"

What is the new behavior?

With the same setup:

$ arduino-cli compile -b arduino:avr:uno --format json | jq .compiler_err
"/home/cmaglie/Arduino/Blink/test.cpp: In function 'void aaa()':\n/home/megabug/Arduino/Blink/test.cpp:2:12: error: expected '}' at end of input\n void aaa() {\n            ^\n\n"

Does this PR introduce a breaking change, and is titled accordingly?

No

Other information

@cmaglie cmaglie self-assigned this Feb 17, 2023
@cmaglie cmaglie added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Feb 17, 2023
@cmaglie cmaglie added this to the Arduino CLI 1.0 milestone Feb 17, 2023
@cmaglie cmaglie force-pushed the fix_compiler_output branch from c293b1c to d2c4306 Compare February 17, 2023 16:12
@codecov
Copy link

codecov bot commented Feb 17, 2023

Codecov Report

Patch coverage: 73.68% and project coverage change: +0.01 🎉

Comparison is base (6992de7) 36.45% compared to head (8b73eee) 36.46%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2078      +/-   ##
==========================================
+ Coverage   36.45%   36.46%   +0.01%     
==========================================
  Files         229      229              
  Lines       19588    19610      +22     
==========================================
+ Hits         7140     7151      +11     
- Misses      11611    11618       +7     
- Partials      837      841       +4     
Flag Coverage Δ
unit 36.46% <73.68%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
legacy/builder/container_find_includes.go 57.67% <0.00%> (ø)
legacy/builder/preprocess_sketch.go 0.00% <0.00%> (ø)
legacy/builder/utils/utils.go 52.57% <50.00%> (-2.07%) ⬇️
legacy/builder/types/context.go 71.60% <85.71%> (-1.53%) ⬇️
legacy/builder/builder.go 84.84% <100.00%> (ø)
legacy/builder/builder_utils/utils.go 62.60% <100.00%> (+0.88%) ⬆️

... and 2 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -184,29 +184,30 @@ const (
)

func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int) ([]byte, []byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got inspired by this refactoring, but do what you want with it. Since it took longer than expected it might not be that good

func getWriter(option int, isVerbose bool, candidate io.Writer) io.Writer {
	getCandidateOrStdout := func() io.Writer {
		if candidate != nil {
			return candidate
		}
		return os.Stdout
	}
	mapOptionToWriterFunc := map[int]func() io.Writer{
		Ignore: func() io.Writer {
			return io.Discard
		},
		Show: getCandidateOrStdout,
		ShowIfVerbose: func() io.Writer {
			if !isVerbose {
				return io.Discard
			}
			return getCandidateOrStdout()
		},
		Capture: func() io.Writer {
			return &bytes.Buffer{}
		},
	}
	return mapOptionToWriterFunc[option]()
}

func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int) ([]byte, []byte, error) {
	stdoutWriter := getWriter(stdout, ctx.Verbose, ctx.Stdout)
	stderrWriter := getWriter(stderr, ctx.Verbose, ctx.Stderr)
	return ExecCommandWithWriters(ctx, command, stdoutWriter, stderrWriter)
}

func ExecCommandWithWriters(ctx *types.Context, command *exec.Cmd, stdout io.Writer, stderr io.Writer) ([]byte, []byte, error) {
	if ctx.Verbose {
		ctx.Info(PrintableCommand(command.Args))
	}
	command.Stdout = stdout
	command.Stderr = stderr

	err := command.Start()
	if err != nil {
		return nil, nil, errors.WithStack(err)
	}

	err = command.Wait()

	var outbytes, errbytes []byte
	if buf, ok := command.Stdout.(*bytes.Buffer); ok {
		outbytes = buf.Bytes()
	}
	if buf, ok := command.Stderr.(*bytes.Buffer); ok {
		errbytes = buf.Bytes()
	}

	return outbytes, errbytes, errors.WithStack(err)
}```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something to keep in mind when this subroutine will be merged with executils and removed from legacy.

@cmaglie cmaglie force-pushed the fix_compiler_output branch from d2c4306 to 8b73eee Compare March 10, 2023 13:34
@cmaglie cmaglie merged commit eece582 into arduino:master Mar 10, 2023
@cmaglie cmaglie deleted the fix_compiler_output branch March 10, 2023 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect compiler_err field contents from compile --format json when compiling .cpp file
3 participants